package common.heap;

/**
 * 1143. 最长公共子序列
 */
public class _1143_LongestCommonSubsequence {

    public static int longestCommonSubsequence(String text1, String text2) {
        if (text1 == null || text2 == null) {
            return 0;
        }
        int m = text1.length();
        int n = text2.length();
        int[][] ary = new int[m + 1][n + 1];
        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= n; j++) {
                if (text1.charAt(i - 1) == text2.charAt(j - 1)) {
                    ary[i][j] = ary[i - 1][j - 1] + 1;
                } else {
                    ary[i][j] = Math.max(ary[i - 1][j], ary[i][j - 1]);
                }
            }
        }
        return ary[m][n];
    }

    public static void main(String[] args) {
        int num = longestCommonSubsequence("abc", "def");
        System.out.println(num);
    }
}
